<?php

namespace App\Http\Controllers\H5;

use App\Library\Mall\Item\ItemTrait;
use App\Models\Item;
use Illuminate\Http\Request;

class ItemController extends BaseController
{
    use ItemTrait;

    public function index()
    {

    }

    public function detail($itemid)
    {
        $item = Item::find($itemid);
        if ($item) {
            $item->incrementViews();
            if ($item->on_sale) {
                $catlog  = $item->catlog;
                $content = $item->content;
                $images  = $item->images;
                $shop    = $item->shop;
                $reviews = $item->buyerReviews()->with('user')
                    ->limit(5)->orderByDesc('id')
                    ->get()->map(function ($review){
                        $review->images = $review->images ? unserialize($review->images) : [];
                        $review->add_images = $review->add_images ? unserialize($review->add_images) : [];
                        return $review;
                    });

                $product_score = $shop->evaluates()->average('product_score');
                $wuliu_score = $shop->evaluates()->average('wuliu_score');
                $service_score = $shop->evaluates()->average('service_score');

                $this->assign(compact('itemid', 'item', 'catlog', 'content', 'images', 'shop', 'reviews', 'product_score','wuliu_score','service_score'));
                return $this->view('h5.item.detail');
            } else {
                return $this->view('h5.item.offsale');
            }
        } else {
            return $this->view('h5.item.offsale');
        }
    }

    public function reviews(Request $request)
    {
        $itemid = $request->input('itemid', 0);
        $item = Item::find($itemid);
        $total = $item->buyerReviews()->count();
        $imgCount = $item->buyerReviews()->where('image_count', '>', 0)->count();
        $addCount = $item->buyerReviews()->where('add_at', '>', 0)->count();
        return $this->view('h5.item.reviews', compact('itemid', 'item', 'total', 'imgCount', 'addCount'));
    }

    public function getReviews(Request $request)
    {
        $itemid = $request->input('itemid', 0);
        $offset = $request->input('offset', 0);
        $count  = $request->input('count', 20);
        $filter = $request->input('filter', '');

        $item = Item::find($itemid);
        $query = $item->buyerReviews()->with('user');
        if ($filter == 'img')
        {
            $query = $query->where('image_count', '>' , 0);
        }

        if ($filter == 'add')
        {
            $query = $query->where('add_at', '>' , 0);
        }
        $items = $query->offset($offset)->limit($count)->orderByDesc('id')
            ->get()->map(function ($review){
                $review->avatar = avatar($review->uid);
                $review->images = $review->images ? unserialize($review->images) : [];
                if ($review->images)
                {
                    $review->images = collect($review->images)->map(function ($img){
                        return image_url($img);
                    });
                }
                $review->add_images = $review->add_images ? unserialize($review->add_images) : [];
                if ($review->add_images)
                {
                    $review->add_images = collect($review->add_images)->map(function ($img){
                        return image_url($img);
                    });
                }
                $review->created_at = @date('Y.m.d H:i', $review->created_at);
                $review->add_at = $review->add_at ? @date('Y.m.d H:i', $review->add_at) : null;
                if (!$review->user)
                {
                    $review->user = [];
                }
                return $review;
            });

        return ajaxReturn(['items'=>$items]);
    }
}
